package com.auicyh.tylb.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.auicyh.tylb.R;
import com.auicyh.tylb.utils.LogUtils;

/**
 * 作者：  Ocean 2018/8/23 15:45
 * 信息：  Oil com.auicyh.tylb.view
 * 功能：  简单的提升对话框,对话框最大宽度400dp,默认根据内容调整高度，把isHighContent置为false后可设置mHigh的调整高度
 * 邮箱：   1126067113@qq.com
 * 修改人：
 * 公司: 北京天元陆兵汽车科技有限公司
 * @author Ocean
 */
public class DialogCustomFragment extends BaseDialogFragment {

    /** 可变色 */
    private SpannableStringBuilder mSpannableString;
    /** 是否变色 */
    private boolean mDiscolor = false;

    /**
     * @param title    标题栏
     * @param message  提示信息
     * @param positive 积极的按钮
     * @param negative 消极的按钮
     * @return DialogCustomFragment对象
     */
    public static DialogCustomFragment newInstance(String title, String message, String positive, String negative) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_DIALOG_TITLE, title);
        bundle.putString(KEY_DIALOG_MESSAGE, message);
        bundle.putString(KEY_DIALOG_POSITIVE, positive);
        bundle.putString(KEY_DIALOG_NEGATIVE, negative);
        DialogCustomFragment dialogFragment = new DialogCustomFragment();
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    /**
     * @param activity activity
     * @param title    标题栏
     * @param message  提示信息
     * @param positive 积极的按钮
     * @param negative 消极的按钮
     * @return DialogCustomFragment对象
     */
    public static DialogCustomFragment newInstance(Activity activity, @StringRes int title, @StringRes int message, @StringRes int positive, @StringRes int negative) {
        return newInstance(activity.getString(title), activity.getString(message), activity.getString(positive), activity.getString(negative));
    }

    /**
     * @param title   标题栏
     * @param message 提示信息
     * @return DialogCustomFragment对象
     */
    public static DialogCustomFragment newInstance(String title, String message) {
        return newInstance(title, message, "确定", "取消");
    }

    /**
     * @param activity activity
     * @param title    标题栏
     * @param message  提示信息
     * @return DialogCustomFragment对象
     */
    public static DialogCustomFragment newInstance(Activity activity, @StringRes int title, @StringRes int message) {
        return newInstance(activity.getString(title), activity.getString(message), "确定", "取消");
    }

    /**
     * @param message 提示信息
     * @return DialogCustomFragment对象
     */
    public static DialogCustomFragment newInstance(String message) {
        return newInstance("温馨提示", message, "确定", "取消");
    }

    /**
     * @param activity activity
     * @param message  提示信息
     * @return DialogCustomFragment对象
     */
    public static DialogCustomFragment newInstance(Activity activity, @StringRes int message) {
        return newInstance("温馨提示", activity.getString(message), "确定", "取消");
    }

    public DialogCustomFragment() {
        setHighContent(true);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            setTitle(bundle.getString(KEY_DIALOG_TITLE));
            setMessage(bundle.getString(KEY_DIALOG_MESSAGE));
            setPositive(bundle.getString(KEY_DIALOG_POSITIVE));
            setNegative(bundle.getString(KEY_DIALOG_NEGATIVE));
        }
    }


    @Override
    public void onViewDialog(FrameLayout frameLayout) {
        View view = View.inflate(getActivity(), R.layout.dialog_custom_fragment, null);
        TextView textView = view.findViewById(R.id.tylb_dialog_custom_message_tv);
        frameLayout.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textView.setGravity(getGravity());
        LogUtils.e(true, getMessage().length() + " \t" + mDiscolor);
        if (mDiscolor) {
            textView.setText(mSpannableString);
        } else {
            String mess = getMessage();
            if (mess.length() > 250) {
                mess = "\n\n" + mess;
            }
            textView.setText(mess);
        }
    }

    public SpannableStringBuilder getSpannableString() {
        return mSpannableString;
    }

    public void setSpannableString(SpannableStringBuilder spannableString) {
        setMessage("");
        mSpannableString = spannableString;
    }

    public boolean isDiscolor() {
        return mDiscolor;
    }

    public void setDiscolor(boolean discolor) {
        mDiscolor = discolor;
    }
}
